Skip to content

Instantly share code, notes, and snippets.

@willwhui
Last active April 18, 2022 10:14
Show Gist options
  • Save willwhui/861bf7eaa713935cf9ce2b1f3cd919e7 to your computer and use it in GitHub Desktop.
Save willwhui/861bf7eaa713935cf9ce2b1f3cd919e7 to your computer and use it in GitHub Desktop.
MacBookAir6,2安装Ubuntu的小坑填法
@willwhui
Copy link
Author

willwhui commented Jun 22, 2017

Macbook的ctrl键太小,按键不方便的问题

这纯属个人感受。
我用了多年的mac,很少用到它键盘上的那个ctrl键。
在mac os下,复制粘贴等等都是super+c, super+v。(就是command+c,command+v)
现在装上ubuntu之后,复制粘贴等等变成了ctrl+c, ctrl+v
总是按不到,并且一不小心按下左边的super键,就呼出了ubuntu的“本地+在线搜索”,神烦。。。
于是按照这里https://askubuntu.com/questions/254424/how-can-i-change-what-keys-on-my-keyboard-do-how-can-i-create-custom-keyboard/706210#706210 说的方法,把左super键和左ctrl键的映射位置做了交换。
爽了。

Using xkb
xkb is the X Keyboard extension. It is responsible for mapping your keyboard's keys to their designated function. xkb symbols can be found in /usr/share/X11/xkb/symbols/. The modifier keys are mapped in /usr/share/X11/xkb/symbols/pc.
Say, I need to switch the functions of Caps Lock and Left Control keys. These changes are required to the pc file:
key { [ Control_L ] };
key { [ Caps_Lock ] };
In order to apply the new mappings you need to clear out xkb’s cache from /var/lib/xkb:
rm -rf /var/lib/xkb/*
Now, just restart your X session and you are good to go.
Source: https://radu.cotescu.com/remapping-keys-in-ubuntu-14.04/
Edit: Doing an ls inside the symbols directory gives this:
af cm gn kz nec_vndr sk
al cn gr la ng sn
altwin compose group latam nl sony_vndr
am ctrl hp_vndr latin no srvr_ctrl
apl cz hr level3 nokia_vndr sun_vndr
ara de hu level5 np sy
at digital_vndr ie lk olpc terminate
az dk il lt pc th
ba ee in lv ph tj
bd empty inet ma pk tm
be epo iq macintosh_vndr pl tr
bg es ir mao pt tw
br et is md ro typo
brai eurosign it me rs tz
bt fi jp mk ru ua
bw fo ke ml rupeesign us
by fr keypad mm se uz
ca fujitsu_vndr kg mn sgi_vndr vn
capslock gb kh mt sharp_vndr xfree68_vndr
cd ge kpdl mv shift za
ch gh kr nbsp si
The mappings inside the file ru are based on the Russian standard keyboard. The macintosh_vndr folder contains the mappings for Apple keyboards. In a nutshell, this approach is so much versatile. Go ahead and play with it :)

@willwhui
Copy link
Author

willwhui commented Jun 22, 2017

盒盖之后,再次唤醒,屏幕不亮的问题

这个问题在官方文档MacBookAir (6,2) on Ubuntu 14.04 (Trusty Tahr)中有解决方案。
搜索“Backlight”可见。
但是我上次按照上面推荐的安装deb的方法操作后效果不是很好(具体问题忘记了)
后来是点击上面页面中的链接Patrik Jakobsson wrote a kernel module
进入到作者的github,按照他说明的操作手动编了一下,就解决问题了。
编一下其实也挺快的。

@willwhui
Copy link
Author

willwhui commented Jun 22, 2017

显示命令行的提示字符串颜色

vi ~/.bashrc
有一句
#force_color_prompt=yes
把'#'去掉,解除注释
如果需要修改提示符的颜色,则修改
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
这一行的32m,00m等。具体数值请google。
比如改成
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
提示符字符串颜色就变成了黄色

保存,并重新打开命令行终端窗口

@willwhui
Copy link
Author

willwhui commented Jun 22, 2017

修改命令行的提示字符串内容

我不希望显示那么多的子目录
vi ~/.bashrc
参照Linux(Ubuntu)终端 命令提示符太长 怎么办?进行修改,

PS1=
这一句里面的\w改成\W(大写)
保存,并重新打开命令行终端窗口

@willwhui
Copy link
Author

willwhui commented Jun 22, 2017

盒盖之后并不挂起的问题

首先,在配置文件
/etc/systemd/logind.conf
中,增加这一句:
HandleLidSwitchDocked=suspend
然后试试看可不可以。

如果不可以,就在命令行中执行
echo "XHC1" > /proc/acpi/wakeup
并且在/etc/rc.local中加入:
echo "XHC1" > /proc/acpi/wakeup
这个XHC1可能因机器不同,见下文。

历程

我通过thunderbolt接了外接显示器。
不正常的表现是盒盖之后是挂起了,但又马上自动唤醒了:外接显示器亮了,显示登录界面。
根据这里的指引,在配置文件
/etc/systemd/logind.conf
中,增加这一句:
HandleLidSwitchDocked=suspend

保存,重启,并没有解决问题。

经过搜索,发现可能是被thunderbolt唤醒的。
按照这里的说法,通过在 配置文件
/etc/rc.local
中一次性加入

echo "EHC1" > /proc/acpi/wakeup
echo "EHC2" > /proc/acpi/wakeup
echo "XHC" > /proc/acpi/wakeup

可以禁止usb的唤醒。

但是,想继续深挖一下/proc/acpi/wakeup是什么。
发现通过在终端输入:
cat /proc/acpi/wakeup
可以列出当前导致唤醒的设备有哪些。
我的:

Device	S-state	  Status   Sysfs node
P0P2	  S3	*disabled
EC	  S4	*disabled  platform:PNP0C09:00
HDEF	  S3	*disabled  pci:0000:00:1b.0
RP01	  S3	*disabled  pci:0000:00:1c.0
RP02	  S3	*disabled  pci:0000:00:1c.1
RP03	  S3	*disabled  pci:0000:00:1c.2
ARPT	  S4	*disabled  pci:0000:03:00.0
RP05	  S3	*disabled  pci:0000:00:1c.4
RP06	  S3	*disabled  pci:0000:00:1c.5
SPIT	  S3	*disabled
XHC1	  S3	*enabled   pci:0000:00:14.0
ADP1	  S4	*disabled  platform:ACPI0003:00
LID0	  S4	*enabled   platform:PNP0C0D:00

发现这里面并没有EHC1等等东西,于是猜测只需要在 /etc/rc.local中加入:
echo "XHC1" > /proc/acpi/wakeup

目前看起来这样一句就可以了,重启后,再次cat /proc/acpi/wakeup,可以看到:
XHC1 S3 *enabled pci:0000:00:14.0
变成了
XHC1 S3 *disabled pci:0000:00:14.0
简单测试后,发现问题解决。

但是,因为意外导致重新安装desktop (sudo apt-get install ubuntu-desktop)之后,以上配置都失效了...
解除/etc/systemd/logind.conf文件中的HandleLidSwitch=suspend这句的注释之后,重启,又好了。
莫名其妙。

@willwhui
Copy link
Author

休眠后应用程序会跑到看不见的workspace中去

参见这里的bug记录
已经有几年了,貌似无解?

@willwhui
Copy link
Author

willwhui commented Jun 25, 2017

电池模式省电配置

按照Finetuning Powersave functions配置了一下,主观感觉:
1、(好像)没多大效果
2、(可能)导致连接到手机的蓝牙共享网络时经常自动断网,必须重启计算机才能再次连接。
卸载掉tlp之后,好像恢复正常了。
不知道原因,以后再折腾吧。

@willwhui
Copy link
Author

willwhui commented Jun 29, 2017

不要卸载python3.4

卸载会导致wifi立即不可用,重启后无法进入图形界面。
需要连接网线,按照ubuntu官方的指引接入局域网。(注:不需要操作链接里面说的修改host之类的)
然后sudo apt-get install ubuntu-desktop,重新装回图形界面。
就恢复正常了。

ubuntu 14.04自带了两个版本的python:2.7和3.4
没有尝试过卸载python2.7,没空做死。

@willwhui
Copy link
Author

Ubuntu系统备份

默认只备份了home/,最好备份/
万一崩溃了可以恢复

@willwhui
Copy link
Author

mount一个分区,创建swap文件

在安装ubuntu之前特地划分了一个分区做swap,但是不知道为什么没有被系统用上。
手动mount:
首先通过disks应用程序查看,发现划分的分区是处于完全未使用状态。
选中它,添加一个分区,并格式化为ext4。
得到了/dev/sda5
然后建立目录/swap
参照这里进行如下操作,开机自动挂载:
sudo gedit /etc/fstab
在文件中添加一行:
/dev/sda5 /swap ext4 defaults 0 2

重启。
命令行运行df,可见分区已经挂载在/swap目录
(但不知为什么sudo fdisk -l看不到/dev/sda5)

接着创建swap文件:
按照这里的说明
采取快速创建的方式,并启动时加载
文章中提到,如果是VPS,应该将swappiness降低到0,减少交换的机会以避免运行过慢。

@willwhui
Copy link
Author

willwhui commented Oct 24, 2017

sd卡在休眠之后就不再识别了

无效的方法:
https://www.reddit.com/r/linuxmint/comments/3ydoxs/unable_to_detect_builtin_sd_card_reader/
sudo vi /etc/default/tlp
为USB_BLACKLIST添加sd card reader(通过lsusb)可以得到编号
然后重启

@willwhui
Copy link
Author

willwhui commented Feb 21, 2022

蓝牙耳机声音卡顿

原因:给耳机的声音缓存不够
解决方案:设置一个适合的缓存

可用的方案:

# .sh file
# display info
pactl list | grep -Pzo '.*bluez_card(.*\n)*'

# set buffer time
sudo pactl set-port-latency-offset bluez_card.XX_XX_XX_XX_XX_XX headphone-output 100

echo ">>>>>> wait for reconnect: <<<<<<"
sudo service bluetooth restart

echo ">>>>>> ***  reconnect in 3 seconds *** <<<<<<"
sleep 1
echo ">>>>>> ** reconnect in 2 seconds ** <<<<<<"
sleep 1
echo ">>>>>> * reconnect in 1 second * <<<<<<"

# reconnect
sudo bluetoothctl connect XX:XX:XX:XX:XX:XX

# display info
pactl list | grep -Pzo '.*bluez_card(.*\n)*'

参见:
https://askubuntu.com/questions/475987/a2dp-on-pulseaudio-terrible-choppy-skipping-audio
As none of the other answers worked on my system (Ubuntu 18.04 LTS on a 2012 MacBook Air), I found my solution on the german ubuntuusers wiki. English summary of the german instructions:

The choppy output might be caused by the A2DP implementation, and how it buffers sound before encoding it. For me, changing this buffer's size solved the choppy sound problem. You need to perform three steps:

Find necessary info about the bluetooth device (while it is connected!)

pactl list | grep -Pzo '.*bluez_card(.*\n)*'

The output should be something like

Name: bluez_card.28_11_A5_84_B6_F9
Driver: module-bluez5-device.c
...
Ports:
speaker-output: Speaker (priority: 0, latency offset: 0 usec, available)
    Part of profile(s): a2dp_sink, headset_head_unit
speaker-input: Bluetooth Input (priority: 0, latency offset: 0 usec, not available)
    Part of profile(s): headset_head_unit

We see that the buffers have currently 0 latency. In the next step, you will need the NAME and PORT of your output. In this example, these are bluez_card.28_11_A5_84_B6_F9 and speaker-output, respectively.

Set the buffer size (latency) of your card to a suitable value with this command pattern:

pactl set-port-latency-offset <NAME> <PORT> <BUFFER_SIZE_MICROSECONDS> 

The latency unit of the following command is microseconds, so I'm using a 50 millisecond buffer for my command here:

pactl set-port-latency-offset bluez_card.28_11_A5_84_B6_F9 speaker-output 50000 

Restart your bluetooth service to apply your change

sudo service bluetooth restart

As there is usually no documentation about this, you may have to experiment with higher or lower buffer values. Many people people posted their working latencies in the comments to this answer. Check them out for guidance on the latency value.

@willwhui
Copy link
Author

willwhui commented Feb 25, 2022

合盖后先 sleep再hibernate

参见这里这里

综合起来这样做,运行:
sudo vi /etc/systemd/sleep.conf
打开这个配置文件后,
去掉AllowSuspendThenHibernate=yes的注释(不过,按道理说默认是yes,应该不用去掉也可以工作)
去掉#HibernateDelaySec=10min一行的注释,改为:
HibernateDelaySec=600
表示suspend后,10分钟之后开始hibernate

运行:
sudo vi /etc/systemd/logind.conf
修改里面的几行为:

HandleLidSwitch=suspend-then-hibernate
HandleLidSwitchExternalPower=suspend-then-hibernate
HandleLidSwitchDocked=suspend-then-hibernate

然后重启对应的服务:
systemctl restart systemd-logind.service

如果现在测试一下hibernate功能的话,会发现:
从休眠状态恢复后,并非休眠前的情况,而是类似于关机重启。

解决之前,为了防止后续的操作导致启动失败,可以先做个准备,让系统启动菜单显示几秒钟,在/etc/default/grub文件中做如下更改:

GRUB_TIMEOUT_STYLE=menu
GRUB_TIMEOUT=2

接下来,开始操作解决休眠恢复无效的方案。
参见这里
上文配置过程中会出现如下警告:

update-initramfs: Generating /boot/initrd.img-5.4.0-100-generic
W: initramfs-tools configuration sets RESUME=UUID=b355eafd-25fb-4934-9c3c-1ca6071659c2
W: but no matching swap device is available.
...

根据这里的说明,是因为文件etc/initramfs-tools/conf.d/resume 中的设置是不必要的,删除或注释调它
#RESUME=UUID=b355eafd-25fb-4934-9c3c-1ca6071659c2 resume_offset=3487744
重新运行
sudo update-initramfs -c -k all

然后重启,测试hibernate功能,看是否成功
sudo systemctl hibernate

注:必须保证有足够大的swapfile,增大swapfile的做法参考这里
注:目前建议的swapfile大小参见这里
注:如果swapfile太小,将无法进入hibernate状态,sudo systemctl suspend-then-hibernatesudo systemctl hibernate 等方法都将不可用,因为不够将内存和已经交换到swapfile的数据同时写入swapfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment